package com.zsrt.day20170427.am;

import java.lang.reflect.Method;

import javax.swing.text.StyledEditorKit.ForegroundAction;

import org.junit.Test;

import com.zsrt.day20170419.am.interfaces.Cal;

/**
*@author jzh
*@创建时间 2017年4月27日下午1:22:13
*类说明：
*/
public class MethodTest {
	/**
	 * 通过反射，拿到方法list，遍历找出后，调用方法
	 * @throws Exception
	 */
	@Test
	public void test1() throws Exception{
		Class clazz = Class.forName("com.zsrt.day20170427.am.Student");
		Object student = clazz.newInstance();
		
		Method[] methods = clazz.getMethods();
		//System.out.println(methods.length);
		
		Method[] methods2 = clazz.getDeclaredMethods();
		System.out.println(methods2.length);
		
		for(Method method:methods2){
			String methodName = method.getName();
			if("cal".equals(methodName)){
				method.setAccessible(true);
				Object obj = method.invoke(student, 10,20);
				System.out.println(obj);
			}
		}
	}
	
	/**
	 * 通过指定的方法名，调用方法
	 * @throws Exception 
	 */
	@Test
	public void test2() throws Exception{
		
		Class clazz = Class.forName("com.zsrt.day20170427.am.Student");
		Object student= clazz.newInstance();
		Method method = clazz.getDeclaredMethod("cal", int.class,int.class);
		method.setAccessible(true);
		Object obj = method.invoke(student, 20,30);
		System.out.println(obj);
		
		
	}
	
	

}
